package org.tinaelle.client;

import java.util.ArrayList;
import java.util.Iterator;

import org.tinaelle.client.utils.ExceptionTool;
import org.tinaelle.common.dto.AdUserDTO;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class AdUser extends AdUserDTO implements AsyncCallback<AdUserDTO> {
	public interface AdUserChangeListener {
		public void userChanged(AdUser user);
	}

	private static final long	serialVersionUID	= 1L;

	private static AdUser	  instance;

	public static AdUser get() {
		if ( instance == null ) {
			instance = new AdUser();
		}
		return instance;
	}

	private transient final ArrayList<AdUserChangeListener>	listeners	= new ArrayList<AdUserChangeListener>();

	public AdUser() {
	}

	public void addChangeListener(final AdUserChangeListener l) {
		listeners.add( l );
	}

	public void onFailure(final Throwable arg0) {
		Window.alert( ExceptionTool.extractUserMessage( arg0 ) );
	}

	public void onSuccess(AdUserDTO u) {
		copy( u );
		for ( final Iterator<AdUserChangeListener> i = listeners.iterator(); i.hasNext(); ) {
			( i.next() ).userChanged( this );
		}
	}

	public void removeChangeListener(final AdUserChangeListener l) {
		listeners.remove( l );
	}

	public void update() {
		// SecurityRService.Util.getInstance().getLoggedInUser(this);
	}
}
